<%@page import="com.dotmedia.entities.SystemEntity"%>
<%@page import="com.dotmedia.dboperations.SystemDatabaseOperations"%>
<%@page import="com.dotmedia.utils.StringUtils"%>
<%@page import="com.dotmedia.dboperations.PermissionDatabaseOperations"%>
<%@page import="com.dotmedia.entities.Permission"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/astele.css">
<link type="text/css" href="css/jquery-ui-1.8.10.custom.css"
	rel="stylesheet" />
<script type="text/javascript" src="javascript/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
	src="javascript/jquery-ui-1.8.10.custom.min.js"></script>
<script type="text/javascript">
	var completeArray = Array();
	var functionTimes = 0;
	function refineRoleSelectBox() {
		var refineText = jQuery("#roleRefineText").val();
		//get all the options of the select
		if (functionTimes == 0) {
			jQuery("#rolesList").find('option').each(function() {
				completeArray.push(jQuery(this).val());
			});
			functionTimes = functionTimes + 1;
		}
		var refinedArray = Array();
		for ( var arrIndex = 0; arrIndex < completeArray.length; arrIndex++) {
			var currentString = completeArray[arrIndex];
			if (currentString.search(refineText) != -1) {
				//add this to refined array
				refinedArray.push(currentString);
			}
		}
		var htmlString = "";
		for ( var refinedArrayIndex = 0; refinedArrayIndex < refinedArray.length; refinedArrayIndex++) {
			htmlString += "<option value ='"+refinedArray[refinedArrayIndex]+"'>"
					+ refinedArray[refinedArrayIndex] + "</option>";
		}
		jQuery("#rolesList").html(htmlString);
	}
	function selectChildCheckboxes(idStart) {
		jQuery('input[id^="'+idStart+'"]').attr('checked', true);
	}
	function deselectChildCheckboxes(idStart) {
		jQuery('input[id^="'+idStart+'"]').attr('checked', false);
	}
	function selectOrDeselectChildren(id) {
		//check whether selected or deselected and then call corresponding function
		if (jQuery('input[id="'+id+'"]').attr('checked') == true) {
			//it is checked
			selectChildCheckboxes(id);
		} else {
			//it is deselected
			deselectChildCheckboxes(id);
		}
	}
	function showAddNewRoleDialog() {
		jQuery("#addNewRoleDialog").dialog(
					{
						height: 400,
						width: 700,
						modal: true
					}
				);
	}
	function validateAddNewRoleEntries() {
		jQuery("#roleDescription").css("background-color", "white");
		jQuery("#roleExternalId").css("background-color", "white");
		var roleDescription = jQuery("#roleDescription").val();
		var roleExtId = jQuery("#roleExternalId").val();
		if (roleDescription.length <= 0) {
			jQuery("#newRoleValidationMessage").html("Role description cannot be empty");
			jQuery("#roleDescription").css("background-color", "red");
			return false;
		} else if (roleExtId.length <= 0) {
			jQuery("#newRoleValidationMessage").html("External id cannot be empty");
			jQuery("#roleExternalId").css("background-color", "red");
			return false;
		}
		return true;
	}
</script>
</head>
<body>
<%
	List<String> allRolesList = (List<String>) request.getAttribute("allRoles");
	if (allRolesList == null) {
%>
<h1>An error has occurred. Please contact the site administrator</h1>
<%
	} else {
%>
<br />
<br />
<button id="addNewRoleButton" name="addNewRoleButton" onclick="showAddNewRoleDialog()">Add new Role</button>
<br />
<br />

<input type="text" size="30" id="roleRefineText" name="roleRefineText"
	onkeyup="refineRoleSelectBox()"></input>
<br />
<select name="rolesList" id="rolesList" size="5">
	<%
		for (int allRolesListIndex = 0; allRolesListIndex < allRolesList.size(); allRolesListIndex++) {
				String currentRole = allRolesList.get(allRolesListIndex);
	%>
	<option value='<%=currentRole%>'><%=currentRole%></option>
	<%
		}
	%>
</select>
<%
	}
%>
<%
	//get all mappings of permission groups with permissions
	PermissionDatabaseOperations permissionDatabaseOperations = new PermissionDatabaseOperations();
	Map<String, List<Permission>> permisisonGroupVsPermissionList = permissionDatabaseOperations.getPermissionGroupMappingWithPermission();
	//get all systems as well
	SystemDatabaseOperations systemDatabaseOperations = new SystemDatabaseOperations();
	List<SystemEntity> allSystemEntities = systemDatabaseOperations.getAllSystemEntities();
%>

<!-- This is the form for creating a new role -->
<div id = "addNewRoleDialog" name = "addNewRoleDialog" title = "Add New Role" style = "display: none;">
<p class = "validateTips" id = "newRoleValidationMessage">All fields are required</p>
<form name = "addNewRole" id = "addNewRole" action = "addNewRole.action" method = "POST" onsubmit="return validateAddNewRoleEntries()">
	<table name="addNewRoleTable" id="addNewTableForm" class="mt_table">
		<tr class="even_td">
			<td></td>
			<td></td>
			<td><b>Notes</b></td>
		</tr>
		<tr class="odd_td">
			<td><b>Description: </b></td>
			<td><input type="text" size="30" name="roleDescription"
				id="roleDescription"></input>
			<td>
			<td rowspan="3"><textarea rows="3" cols="30" name="roleNotes"
				id="notesTextArea"></textarea>
		</tr>
		<tr class="even_td">
			<td><b>External id: </b></td>
			<td><input type="text" size="30" name="roleExternalId"
				id="roleExternalId"></input></td>
		</tr>
		<tr class = "odd_td">
			<td><b>System</b></td>
			<td>
				<select name = "systemId" id = "systemId">
		<%
					for (int allSystemEntitesIndex = 0; allSystemEntitesIndex < allSystemEntities.size(); allSystemEntitesIndex++) {
						String systemExtId = allSystemEntities.get(allSystemEntitesIndex).getSystemExtId();
		%>
						<option value = "<%=systemExtId %>"><%=systemExtId %></option>
		<%
					}
		%>
				</select>
			</td>
		</tr>
	</table>
	<table name="permissionListingTable" id="permissionListingTable"
		class="mt_table">
		<%
			Iterator<String> permissionGroupDescItr = permisisonGroupVsPermissionList.keySet().iterator();
			int globalPermissionIndex = -1;
			while (permissionGroupDescItr.hasNext()) {
				String currentPermissionGroupDesc = permissionGroupDescItr.next();
		%>
		<tr class="even_td">
			<td><input type="checkbox" id="<%=currentPermissionGroupDesc%>"
				onclick="selectOrDeselectChildren('<%=currentPermissionGroupDesc%>')"></input></td>
			<td><%=currentPermissionGroupDesc%></td>
			<td></td>
		</tr>
		<%
			//get all the permissions for this current permission group and display them
				List<Permission> currentPermissionList = permisisonGroupVsPermissionList.get(currentPermissionGroupDesc);
				for (int currentPermissionListIndex = 0; currentPermissionListIndex < currentPermissionList.size(); currentPermissionListIndex++) {
					globalPermissionIndex++;
					Permission currentPermission = currentPermissionList.get(currentPermissionListIndex);
					String permissionDesc = currentPermission.getDescription();
					String permissionNotes = currentPermission.getNotes();
					String permissionId = currentPermission.getPermissionExtId();
					if (StringUtils.isStringNull(permissionNotes)) {
						permissionNotes = "";
					}
		%>
		<tr class="odd_td">
			<td><input type="checkbox" name="permissionIds[]"
				value="<%=permissionId%>"
				id="<%=currentPermissionGroupDesc%>,<%=permissionId%>"></input></td>
			<td><%=permissionDesc%></td>
			<td><%=permissionNotes%></td>
		</tr>
		<%
			}
			}
		%>
	</table>
	<br/>
	<input type = "submit" id = "submitNewRole" name = "submitNewRole" value = "Save"></input>
</form>
</div>
</body>
</html>